<!DOCTYPE html >
<html xmlns:th="http://www.thymeleaf.org" lang="en">
<head>
    <title>用户管理</title>
    <script th:replace="common/head::static"></script>
</head>
<body>

<div class="layui-fluid">
    <div class="layui-card">
        <fieldset class="layui-elem-field">
            <legend>条件搜索</legend>
            <form class="layui-form layui-card-header" id="sysUserSearchForm">
                <div class="layui-form-item">
                    <div class="layui-inline">
                        <label class="layui-form-label">用户名</label>
                        <div class="layui-input-inline">
                            <input type="text" name="username" placeholder="请输入" autocomplete="off" class="layui-input">
                        </div>
                    </div>
                    <div class="layui-inline">
                        <label class="layui-form-label">是否超管</label>
                        <div class="layui-input-inline">
                            <select name="isSuper">
                                <option value="">请选择</option>
                                <option value="Y">是</option>
                                <option value="N">否</option>
                            </select>
                        </div>
                    </div>
                    <div class="layui-inline">
                        <!-- type="button" 阻止默认行为提交form表单 -->
                        <button type="button" class="layui-btn" id="sysUserSearchBtn">
                            <i class="layui-icon layui-icon-search"></i>查询
                        </button>
                        <button type="reset" class="layui-btn layui-btn-primary">重置</button>
                    </div>
                </div>
            </form>
        </fieldset>

        <div class="layui-card-body">
            <div style="padding-bottom: 10px;">
                <button class="layui-btn" id="sysUserAddBtn">
                    <i class="layui-icon layui-icon-add-circle-fine"></i>新增
                </button>
            </div>
            <table class="layui-hide" id="sysUserTable" lay-filter="sysUserTableFilter"></table>
            <script type="text/html" id="toolBars">
                <a class="layui-btn layui-btn-warm layui-btn-xs" lay-event="detail">
                    <i class="layui-icon"></i>查看</a>
                <a class="layui-btn layui-btn-normal layui-btn-xs" lay-event="edit">
                    <i class="layui-icon layui-icon-edit"></i>编辑</a>
                <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">
                    <i class="layui-icon layui-icon-delete"></i>删除</a>
                <a class="layui-btn layui-btn-primary layui-btn-xs layui-bg-gray" lay-event="setSysRole">
                    <i class="layui-icon layui-icon-set-sm"></i>设置角色</a>
            </script>
        </div>
    </div>
</div>

<script th:src="@{/common/js/common.js}"></script>
<script th:inline="javascript">
    layui.use(['table', 'form'], function () {
        const table = layui.table
            , $ = layui.jquery
            , form = layui.form;

        /* templet - 自定义列模板: https://www.layui.com/doc/modules/table.html#templet */
        const cols = [[
            {field: 'checkBox', checkbox: true, fixed: true}
            , {field: 'username', title: '用户名', align: 'center'}
            , {
                field: 'isSuper', title: '是否超管', align: 'center', templet: function (d) {
                    return d.isSuper === 'Y' ? '是' : '否';
                }
            }
            , {
                field: 'deptId', title: '部门', align: 'center', templet: function (d) {
                    if (d.sysDept) {
                        return d.sysDept.name;
                    } else {
                        return '-';
                    }
                }
            }
            , {
                field: 'valid', title: '是否可用', align: 'center', templet: function (d) {
                    return d.valid === 'Y' ? '是' : '否';
                }
            }
            , {field: 'createdBy', title: '创建人', align: 'center'}
            , {field: 'createdDate', title: '创建时间', align: 'center'}
            , {field: 'op', title: '操作', align: 'center', width: '25%', toolbar: '#toolBars'} //这里的toolbar值是模板元素的选择器
        ]];

        const layuiTable = Common.getLayuiTable(table, '#sysUserTable', ctx + '/sysUser/findPage', cols);

        $('#sysUserSearchBtn').on('click', function () {
            Common.reloadTable($('#sysUserSearchForm'), layuiTable);
        });

        //监听工具条 https://www.layui.com/doc/modules/table.html#toolbar
        table.on('tool(sysUserTableFilter)', function (obj) { //注：tool 是工具条事件名，test 是 table 原始容器的属性 lay-filter="对应的值"
            var data = obj.data; //获得当前行数据
            var layEvent = obj.event; //获得 lay-event 对应的值（也可以是表头的 event 参数对应的值）
            var tr = obj.tr; //获得当前行 tr 的 DOM 对象（如果有的话）
            if (layEvent === 'detail') { //查看
                Common.openFrame('/sysUser/toDetail/' + data.id, '查看用户', function () {
                    console.log('toDetail');
                });
            } else if (layEvent === 'del') { //删除
                layer.confirm('真的删除行么', function (index) {
                    Common.ajaxDelete('/sysUser/delete/' + data.id, {}, function (res) {
                        layer.close(index);
                        layer.msg(res.message, {icon: 6}, function () {
                            $('#sysUserSearchBtn').click();
                        });
                    });
                });
            } else if (layEvent === 'edit') { //编辑
                Common.openFrame('/sysUser/toUpdate/' + data.id, '编辑用户', function () {
                    console.log('toUpdate');
                });
            } else if (layEvent === 'setSysRole') {
                Common.openFrame('/sysUser/toSetSysRole/' + data.id, '设置角色', function () {
                    console.log('toSetSysRole');
                });
            }
        });

        $("#sysUserAddBtn").on('click', function () {
            Common.openFrame('/sysUser/toSave', '新增用户', function () {
                console.log('toSave');
            });
        });
    });

</script>
</body>
</html>